#include <QtGui>

#include "gridwidget.h"

GridWidget::GridWidget(QWidget *parent, int iW, int iH, QString type) : QWidget(parent){

	iWidth = iW;
	iHeight = iH;
	strType = type;

	setAcceptDrops(true);
	setMinimumSize(iWidth*50, iHeight*50);
	setMaximumSize(iWidth*50, iHeight*50);

	drawGrid();

}

void GridWidget::clear()
{
    pieceLocations.clear();
    piecePixmaps.clear();
    pieceRects.clear();
    highlightedRect = QRect();
    update();
}

void GridWidget::drawGrid(){

	QPainter painter;
	painter.begin(this);

	painter.setBrush(QColor(255,255,255));
	painter.setPen(Qt::SolidLine);
	
	for(int i=1; i<=iWidth; i++){
		painter.drawLine(0, 50*i, iWidth*50, 50*i);
	}

	for(int i=1; i<=iHeight; i++){
		painter.drawLine(50*i, 0, 50*i, iHeight*50);
	}

	painter.end();

}

void GridWidget::dragEnterEvent(QDragEnterEvent *event){
    if (isValidIcon(event)){
        event->accept();
    } else {
        event->ignore();
    }
}

/*void GridWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
    QRect updateRect = highlightedRect;
    highlightedRect = QRect();
    update(updateRect);
    event->accept();
}*/

void GridWidget::dragMoveEvent(QDragMoveEvent *event){
	
	QRect updateRect = highlightedRect.unite(targetSquare(event->pos()));

	if (isValidIcon(event) && findPiece(targetSquare(event->pos())) == -1) {
		
		highlightedRect = targetSquare(event->pos());
		event->setDropAction(Qt::MoveAction);
		event->accept();
	} else {
		highlightedRect = QRect();
		event->ignore();
	}

	update(updateRect);
}

void GridWidget::dropEvent(QDropEvent *event){
	
	if (isValidIcon(event) && findPiece(targetSquare(event->pos())) == -1) {

		QByteArray pieceData = getIconType(event);
		QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
		QRect square = targetSquare(event->pos());
		QPixmap pixmap;
		
		dataStream >> pixmap;

		piecePixmaps.append(pixmap);
		pieceRects.append(square);

		highlightedRect = QRect();
		update(square);

		event->setDropAction(Qt::MoveAction);
		event->accept();

	} else {
		highlightedRect = QRect();
		event->ignore();
	}
}

int GridWidget::findPiece(const QRect &pieceRect) const{
	
	for (int i = 0; i < pieceRects.size(); ++i) {
		if (pieceRect == pieceRects[i]) {
			return i;
		}
	}
	return -1;
}

/*void GridWidget::mousePressEvent(QMouseEvent *event)
{
	QRect square = targetSquare(event->pos());
	int found = findPiece(square);

	if (found == -1)
		return;

	QPoint location = pieceLocations[found];
	QPixmap pixmap = piecePixmaps[found];
	pieceLocations.removeAt(found);
	piecePixmaps.removeAt(found);
	pieceRects.removeAt(found);

	update(square);

	QByteArray itemData;
	QDataStream dataStream(&itemData, QIODevice::WriteOnly);

	dataStream << pixmap << location;

	QMimeData *mimeData = new QMimeData;
	mimeData->setData("image/townIcon", itemData);

	QDrag *drag = new QDrag(this);
	drag->setMimeData(mimeData);
	drag->setHotSpot(event->pos() - square.topLeft());
	drag->setPixmap(pixmap);

	if (!(drag->start(Qt::MoveAction) == Qt::MoveAction)) {
		pieceLocations.insert(found, location);
		piecePixmaps.insert(found, pixmap);
		pieceRects.insert(found, square);
		update(targetSquare(event->pos()));
	}
}*/

void GridWidget::paintEvent(QPaintEvent *event)
{
	QPainter painter;
    painter.begin(this);
    painter.fillRect(event->rect(), Qt::white);

    if (highlightedRect.isValid()) {
        painter.setBrush(QColor("#ffcccc"));
        painter.setPen(Qt::NoPen);
        painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
    }

    for (int i = 0; i < pieceRects.size(); ++i) {
        painter.drawPixmap(pieceRects[i], piecePixmaps[i]);
    }
    painter.end();
    
    drawGrid();
}

const QRect GridWidget::targetSquare(const QPoint &position) const{
    return QRect(position.x()/50 * 50, position.y()/50 * 50, 50, 50);
}

bool GridWidget::isValidIcon(QDropEvent *event){
	
	if(strType == "Worldmap"){

		if (event->mimeData()->hasFormat("image/townIcon") ||
			event->mimeData()->hasFormat("image/dungeonIcon")){
			return true;
		
		} else {
			return false;	
		}  
			
	}else if(strType == "Building" || strType == "Dungeon"){

		if (event->mimeData()->hasFormat("image/roomIcon")){
			return true;
		} else {
			return false;
		}
		  
	}else{
		return false;			
	}	
}

QByteArray GridWidget::getIconType(QDropEvent *event){
	
	if(strType == "Worldmap"){

		if (event->mimeData()->hasFormat("image/townIcon")){
			
			return event->mimeData()->data("image/townIcon");
			
		} else if(event->mimeData()->hasFormat("image/dungeonIcon")){
			
			return event->mimeData()->data("image/dungeonIcon");
		
		} else {
			return NULL;	
		}  
			
	}else if(strType == "Building" || strType == "Dungeon"){

		return event->mimeData()->data("image/roomIcon");
		  
	}else{
		return NULL;			
	}	
	
}
